package com.czkevin.wusan.gui;

import com.czkevin.cif.CIFItemInstance;
import com.czkevin.cif.CustomItemFramework;
import com.czkevin.cif.crafting.gui.CraftingMatrixControl;
import com.czkevin.cif.crafting.gui.CraftingOutputControl;
import com.czkevin.guicontrolsapi.HeadTools;
import com.czkevin.guicontrolsapi.Window;
import com.czkevin.guicontrolsapi.builtin.AlertWindow;
import com.czkevin.guicontrolsapi.controls.ButtonControl;
import com.czkevin.wusan.items.BaseWuSanItem;
import com.czkevin.wusan.listeners.ItemSwitchListener;
import de.tr7zw.nbtapi.NBTContainer;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.SoundCategory;
import org.bukkit.entity.HumanEntity;
import org.bukkit.inventory.ItemStack;

public class AnswerRipperWindow extends Window {

    private CraftingMatrixControl input = new CraftingMatrixControl(1, 1);

    private ItemStack inputItem;

    private CraftingOutputControl output = new CraftingOutputControl(() -> true);

    public AnswerRipperWindow() {
        super("\uf808\u00a7f\uef03", 8, 5);

        input.setCallback(matrix -> {
            inputItem = matrix[0][0];
        });

        ButtonControl buttonRip = new ButtonControl(HeadTools.HEAD_IRON_CHECK, "\u00a7c\u00a7l撕去答案", "\u00a76点我撕去，威力翻倍！");
        buttonRip.setHandler((who) -> {
            if(!ItemSwitchListener.checkWuSan(inputItem)) return;

            if(output.getOutput() != null && output.getOutput().getType() != Material.AIR) {
                // 还有没有取出来的东西
                return;
            }

            CIFItemInstance instance = CustomItemFramework.getCIFDataFromItem(inputItem);

            if(instance.data != null && instance.data.hasKey(BaseWuSanItem.NBT_ANWSERS_RIPPED) && instance.data.getBoolean(BaseWuSanItem.NBT_ANWSERS_RIPPED)) {
                // 已经撕掉了
                new AlertWindow(AlertWindow.AlertType.ATTENTION, "\u00a7d已经被撕掉了", "\u00a7c\u00a7l这本书已经没有答案了！").open(who, this);
                return;
            }

            NBTContainer newData = new NBTContainer();
            if(instance.data != null) newData.mergeCompound(instance.data);
            newData.setBoolean(BaseWuSanItem.NBT_ANWSERS_RIPPED, true);

            // 设置输出
            ItemStack out = instance.type.generateItemStack(new CIFItemInstance(instance.type, newData), false);
            output.setOutput(out);

            // 消除物品
            if(inputItem.getAmount() > 1) {
                inputItem.setAmount(inputItem.getAmount() - 1);
            } else {
                inputItem = null;
            }
            input.getMatrix()[0][0] = inputItem;
            input.applyMatrixToDisplay();

            // 播放声音
            who.getWorld().playSound(who.getLocation(), Sound.BLOCK_ANVIL_USE, SoundCategory.BLOCKS, 1.0f, 1.2f);
        });

        setControl(1, 2, input);
        setControl(3, 2, buttonRip);
        setControl(5, 2, output);
    }

    @Override
    public void onOpenInternal(HumanEntity humanEntity) {

    }

    @Override
    protected boolean onCloseInternal(HumanEntity humanEntity) {
        return true;
    }
}
